
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package itm.util;

//~--- JDK imports ------------------------------------------------------------

import java.text.MessageFormat;

import java.util.Iterator;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.component.EditableValueHolder;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

/**
 *
 * @author jimmy
 */
public class MessageListener implements PhaseListener {
    public PhaseId getPhaseId() {
        return PhaseId.RENDER_RESPONSE;
    }

    public void beforePhase(PhaseEvent e) {
        FacesContext   fc     = e.getFacesContext();
        UIViewRoot     root   = fc.getViewRoot();
        String         mbName = fc.getApplication().getMessageBundle();
        Locale         locale = root.getLocale();
        ResourceBundle rb     = ResourceBundle.getBundle(mbName, locale);
        Iterator       i      = fc.getClientIdsWithMessages();

        while (i.hasNext()) {
            String      clientId = (String) i.next();
            UIComponent c        = root.findComponent(clientId);
            String      fieldRef = (String) c.getAttributes().get("fieldRef");

            if (fieldRef != null) {
                Iterator j = fc.getMessages(clientId);

                while (j.hasNext()) {
                    FacesMessage fm     = (FacesMessage) j.next();
                    String       detail = fm.getDetail();

                    if ("CONV_ERR_MSG".equals(detail)) {
                        String   custMsgPattern = rb.getString("CUST_CONV_ERR_MSG_detail");
                        Object[] params         = new Object[1];

                        params[0] = ((EditableValueHolder) c).getSubmittedValue();

                        String custMsg = MessageFormat.format(custMsgPattern, params);

                        fm.setDetail(custMsg);
                    } else if ("VALUE_REQ_MSG".equals(detail)) {
                        String custMsg = (String) c.getAttributes().get("custMsg");

                        fm.setDetail(custMsg);
                    }

                    fm.setDetail(fieldRef);
                }
            }
        }
    }

    public void afterPhase(PhaseEvent e) {}
}
